using System;
using Csla;
using Ent.AccesoDatos;
using System.Collections.Generic;
using System.Data;

namespace EntGuayasSLA.BL
{
    [Serializable]
    public class DetalleCosto  : BusinessBase<DetalleCosto>
    {
        #region Business Methods

        public static readonly PropertyInfo<Guid> IdProperty = RegisterProperty<Guid>(p => p.Id);
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }

        public static readonly PropertyInfo<string> ExpedienteLicenciamientoProperty = RegisterProperty<string>(c => c.ExpedienteLicenciamiento);
        public string ExpedienteLicenciamiento
        {
            get { return GetProperty(ExpedienteLicenciamientoProperty); }
            set { SetProperty(ExpedienteLicenciamientoProperty, value); }
        }


        public static readonly PropertyInfo<string> DetalleProperty = RegisterProperty<string>(c => c.Detalle);
        public string Detalle
        {
            get { return GetProperty(DetalleProperty); }
            set { SetProperty(DetalleProperty, value); }
        }
     
        public static readonly PropertyInfo<float> ValorProperty = RegisterProperty<float>(c => c.Valor);
        public float Valor
        {
            get { return GetProperty(ValorProperty); }
            set { SetProperty(ValorProperty, value); }
        }

        #endregion

        #region Business Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            base.AddBusinessRules();

            //BusinessRules.AddRule(new Rule(IdProperty));
        }

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //BusinessRules.AddRule(...);
        }

        #endregion

        #region Factory Methods

        public static DetalleCosto  NewEditableRoot()
        {
            return DataPortal.Create<DetalleCosto >();
        }

        public static DetalleCosto  GetEditableRoot(Guid id)
        {
            return DataPortal.Fetch<DetalleCosto >(id);
        }

        public static DetalleCosto GetEditableRoot(string idExpLic)
        {
            return DataPortal.Fetch<DetalleCosto>(idExpLic);
        }
       
        public static void DeleteEditableRoot(Guid id)
        {
            DataPortal.Delete<DetalleCosto>(id);
        }

        private DetalleCosto ()
        { /* Require use of factory methods */ }

        #endregion

        #region Data Access

        [RunLocal]
        protected override void DataPortal_Create()
        {
            // TODO: load default values
            // omit this override if you have no defaults to set
            base.DataPortal_Create();
        }

        private void DataPortal_Fetch(Guid criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_dec_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_DETALLECOST\"", parametros);
            LoadProperts(datos);
        }
        private void DataPortal_Fetch(string criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { new Parametro("ec_eli_id", criteria.ToString()) };
            DataSet datos = accesoDatos.EjecutarDataSet("\"OBT_DETALLECOSTEXP\"", parametros);
            LoadProperts(datos);
        }
        private void LoadProperts(DataSet datos)
        {
            float tmpFloat = 0;
            if (datos.Tables[0].Rows.Count > 0)
            {
                LoadProperty(IdProperty, Guid.Parse(datos.Tables[0].Rows[0]["dec_id"].ToString()));
                LoadProperty(ExpedienteLicenciamientoProperty, datos.Tables[0].Rows[0]["eli_id"].ToString());
               
                LoadProperty(DetalleProperty,datos.Tables[0].Rows[0]["dec_detalle"].ToString());
                float.TryParse(datos.Tables[0].Rows[0]["dec_valor"].ToString(), out tmpFloat);
                LoadProperty(ValorProperty, tmpFloat);

            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {  
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_dec_id", ReadProperty(IdProperty)),
                new Parametro("ec_eli_id", ReadProperty(ExpedienteLicenciamientoProperty)),
                new Parametro("ec_dec_detalle", ReadProperty(DetalleProperty)),
                new Parametro("ed_dec_valor", ReadProperty(ValorProperty))
            };
            accesoDatos.EjecutarQuery("\"INS_DETALLECOST\"", parametros);

        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
          
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                 new Parametro("ec_dec_id", ReadProperty(IdProperty)),
                 new Parametro("ec_dec_detalle", ReadProperty(DetalleProperty)),
                 new Parametro("ed_dec_valor", ReadProperty(ValorProperty))
            };
            accesoDatos.EjecutarQuery("\"ACT_DETALLECOST\"", parametros);
     
        }
      
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(this.ExpedienteLicenciamiento);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(string criteria)
        {
            var accesoDatos = new DriveDataBase(TypeDataBase.PostgreSql);
            var parametros = new List<Parametro> { 
                new Parametro("ec_eli_id", criteria)
            };
            accesoDatos.EjecutarQuery("\"ELI_DETALLECOST\"", parametros);
      
        }

        #endregion
    }
}
